package model;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.tool.schema.TargetType;
import java.util.EnumSet;
import java.util.List;
public class DBUtil {
    private static Configuration cfg;
    private static SessionFactory sf;
    private static Session session;
    public DBUtil() {
        org.hibernate.service.ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().configure().build();
        Metadata metadata = new MetadataSources(serviceRegistry).buildMetadata();
        SchemaExport schemaExport = new SchemaExport();
        schemaExport.create(EnumSet.of(TargetType.DATABASE), metadata);//TargetType是Hibernate自带对象

        cfg = new Configuration();
        sf = cfg.configure().buildSessionFactory();
        session = sf.openSession();
    }

    @Override
    protected void finalize(){
        session.close();
        sf.close();
        System.out.println("in finalize");
    }

    public static void SetPersons(List<Person> persons) {
        for (Person p : persons) {
            SaveToDB(p);
        }
    }
    public static void SetQuestions(List<Question> questions){
        for (Question q : questions){
            SaveToDB(q);
        }
    }
    public static void SetResluts(List<Result> results){

        for (Result r : results){
            SaveToDB(r);
        }
    }

    public static List<Question> LoadQuestions(){

        session.beginTransaction();

        String hql = " from Question"; //from对象名，不是表名
        Query query = session.createQuery(hql);
        List<Question> questions = query.list();
//        for (Question q : questions){
//            System.out.println(q.getTitle());
//            System.out.println(q.getTitle_en());
//        }

        session.getTransaction().commit();

        return questions;
    }
    public static List<Person> LoadPersons(){
        session.beginTransaction();

        String hql = " from Person"; //from对象名，不是表名
        Query query = session.createQuery(hql);
        List<Person> persons = query.list();

        session.getTransaction().commit();
        return persons;
    }

    public static void DeletePersons(){
        session.beginTransaction();

        String hql = " from Person "; //from对象名，不是表名
        Query query = session.createQuery(hql);
        List<Person> persons = query.list();
        for (Person p : persons){
            session.delete(p);
        }

        session.getTransaction().commit();
    }
    public static void DeleteQuestions(){
        session.beginTransaction();

        String hql = " from Question"; //from对象名，不是表名
        Query query = session.createQuery(hql);
        List<Question> questions = query.list();
        for (Question q : questions){
            session.delete(q);
        }

        session.getTransaction().commit();
    }

    public static void SaveToDB(Object obj){
        session.beginTransaction();

        session.save(obj);

        session.getTransaction().commit();

        System.out.print("save success");
    }

    public static void ReadWriteDB(){

        Person p = new Person();

        session.beginTransaction();

        //增操作
//        p.setId(3);
//        p.setName("su");
//        session.save(p);

        //读操作
        //类名.class获得Class实例时，并不会触发类的初始化
        //serializable参数，填入主键，即可获得。
        p = session.get(Person.class, 3); //get(类名.class,)
        System.out.print(p.getName());

        //改操作,只要确定主键，就能修改相应记录
//        p.setName("zhu");
//        session.update(p);

        //删操作,只要确定主键，就能修改相应记录。需要用new一个新对象。
//        Person p2 = new Person();
//        p2.setId(9);
//        session.delete(p2);
//        Query query = session.createQuery("select p.name from Person p");
//        List list = query.list();
//        for(int i = 0 ; i < list.size(); i++)
//        {
//            String name = (String)list.get(i);
//            System.out.println(name);
//        }

        session.getTransaction().commit();
    }
}
